home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 5
/
Apprentice-Release5.iso
/
Source Code
/
C
/
Snippets
/
PPP_PrefSaver 1.1
/
C Source
/
Fetch_Settings.c
next >
Wrap
Text File
|
1996-05-18
|
9KB
|
296 lines
/* Fetch_Settings.c
Simple dialog routines to handle receiving input of a user's account name
and password, which are then placed in the user's PPP connect settings,
used to log onto the service.
The password and account name are entered into the connect script for the
active config.
Correction in 1.0.1: The dialog item numbers for the account name and password
were corrected to the appropriate item numbers. The numbers changed in the process
of editing the dialog and I didn't spot it.
Changes in 1.0.1: The resources for the dialogs and alerts were changed to fit specifications
given in the InstallerMaker 3.0 users guide.
Added in 1.1: Option to save account name/password into the Authentication settings
instead of in a connect script. Also added #define NAME_COMMAND and PWORD_COMMAND
k-onstants to select fields of connect script to write to.
Corrections in 1.1: SimpleModalFilter was fixed to correctly prevent users
from entering illegal characters.
Added check to make sure OK button is actually enabled before making a carriage
return into a hit on the OK button in SimpleModalFilter().
See #define STANDALONE in the PPP_Prefs.h to change from compiling as a standalone
application to compiling as a code resource of type 'IEnd'.
*/
#include "PPP-Prefs.h"
#define kMoveToFront (WindowPtr)-1L
#define kDisable 255
#define kEnable 0
#define kReturnKey 0x0D
#define kTildeKey 0x7E
#define kEnterKey 0x03
#define kESCKey 0x1B
#define kMaxCharLimit 8 // Maximum #chars allowed in name/password
/* THESE WERE REMOVED IN VERSION 1.0.2 FROM SimpleModalFilter() routine */
// #define kPrintCharStart 0x20
// #define kPrintCharEnd 0x7E
#define kCtrlCharEnd 0x1F
#define kDeleteKey 0x7F
// numeric chars top/bottom limits
#define kNumeralZero 0x30
#define kNumeralNine 0x39
// Lowercase alphabetic chars top/bottom limits
#define kSmall_a 0x61
#define kSmall_z 0x7A
// Uppercase letters will be converted to lowercase
#define kBig_A 0x41
#define kBig_Z 0x5A
#define kUpperToLowerCaseOffset 0x20
#define kMyButtonDelay 8
#define kSimpleModalFilter (ProcPtr)&SimpleModalFilter
#define rUserPrefsDialog 3000
#define defOutline 3
#define iUserName 5
#define iPassword 6
#define rConfirmSettingsAlrt 5001
/* Prototypes */
Boolean GetUserPrefs( struct ppp_config *theConfig, OSErr *err );
pascal Boolean SimpleModalFilter( DialogPtr dialog, EventRecord *event,
short *itemHit );
pascal void buttonProc( DialogPtr theDialog, short theItem);
/*************************** GetUserPrefs *********************************/
Boolean GetUserPrefs( struct ppp_config *theConfig, OSErr *err )
{
DialogPtr dialog;
GrafPtr savePort;
Boolean dialogDone = false, saveConfig = false;
short iType, iHit;
Handle iHandle, nameHandle, pwordHandle;
ControlHandle okButton;
Rect iRect;
Str255 theString, the_otherString;
Point topCorner;
GetPort(&savePort);
dialog = GetNewDialog(rUserPrefsDialog, nil, kMoveToFront);
*err = LMGetResErr();
if (!dialog) { goto getOut; }
SetWRefCon(dialog, (long)GetUserPrefs);
SetPort(dialog);
GetDItem(dialog, defOutline, &iType, &iHandle, &iRect);
SetDItem(dialog, defOutline, iType, buttonProc, &iRect);
// Disable "OK" until edit fields are filled in
GetDItem(dialog, ok, &iType, &okButton, &iRect);
HiliteControl(okButton, kDisable);
GetDItem(dialog, iUserName, &iType, &nameHandle, &iRect);
GetDItem(dialog, iPassword, &iType, &pwordHandle, &iRect);
ShowWindow(dialog);
while (! dialogDone)
{
ModalDialog(kSimpleModalFilter, &iHit);
switch( iHit)
{
case ok:
HideWindow(dialog);
GetIText(nameHandle, theString);
GetIText(pwordHandle, the_otherString);
ParamText(theString, the_otherString, 0,0);
iHit = Alert(rConfirmSettingsAlrt,kSimpleModalFilter);
if (iHit == ok)
dialogDone = true;
else
ShowWindow(dialog);
break;
case cancel:
dialogDone = true;
break;
default:
GetIText(nameHandle, theString);
if (theString[0])
GetIText(pwordHandle, theString);
if (! theString[0]){
if ((*okButton)->contrlHilite == kEnable)
HiliteControl(okButton, kDisable);
}
else{
if ((*okButton)->contrlHilite == kDisable)
HiliteControl(okButton, kEnable);
}
break;
}
}
#if ! DEMO_ONLY
if (iHit == ok)
{
/* Write the user's settings into the appropriate fields of the connect
script. There are 8 fields total - theConfig->commands[0-7].scriptstr
(See NUMCOMMANDS in the PPP_Prefs.h) You select which field through
#defines in PPP_Prefs.h as NAME_COMMAND and PWORD_COMMAND
*/
GetIText(nameHandle, theString);
#if USE_SCRIPT
BlockMove(theString, &(theConfig->commands[NAME_COMMAND].scriptstr), theString[0]+1);
#else // Write into Authentication settings instead
BlockMove(theString, &(theConfig->defaultid), theString[0]+1);
#endif
GetIText(pwordHandle, theString);
#if USE_SCRIPT
BlockMove(theString, &(theConfig->commands[PWORD_COMMAND].scriptstr), theString[0]+1);
#else // Write into Authentication settings instead
BlockMove(theString, &(theConfig->defaultpw), theString[0]+1);
#endif
saveConfig = true;
}
#endif
SetPort(savePort);
DisposDialog(dialog);
getOut:
return (saveConfig);
}
/************************* SimpleModalFilter ********************************/
pascal Boolean SimpleModalFilter( DialogPtr dialog, EventRecord *event,
short *itemHit )
{
Boolean returnVal = false;
WindowPtr temp;
ProcPtr *standardProc;
short iType, iHit;
Handle iHandle;
Rect iRect;
char theChar;
Str255 iString;
long waitTicks;
/* This filter is used to queue the ok/cancel buttons via the keyboard
while remaining compatible with System 6. It also limits the user
to entering no more than 8 alphanumeric characters in the edit text fields */
if((event->what == keyDown) || (event->what == autoKey))
{
theChar = (event->message & charCodeMask);
switch (theChar)
{
case kReturnKey:
case kEnterKey:
/* change whatever is the current item to the OK item */
GetDItem(dialog,OK,&iType,&iHandle,&iRect);
if ((**(ControlHandle)iHandle).contrlHilite != kDisable){
*itemHit = OK;
HiliteControl((ControlHandle)iHandle,inButton);
Delay(kMyButtonDelay,&waitTicks); /* wait about 8 ticks */
HiliteControl((ControlHandle)iHandle,false);
returnVal = true;
}
else
event->what = nullEvent;
break;
case kESCKey:
case kTildeKey:
*itemHit = cancel;
GetDItem(dialog,cancel,&iType,&iHandle,&iRect);
HiliteControl((ControlHandle)iHandle,inButton);
Delay(kMyButtonDelay,&waitTicks);
HiliteControl((ControlHandle)iHandle,false);
returnVal = true;
break;
default:
if (GetWRefCon(dialog) == (long)GetUserPrefs) // Ignore the confirm alert
{
/***** ERRRONEOUS CODE REMOVED: I WROTE THIS THINKING I COULD USE THE SAME PARAMETERS
USED BY the isprint() ROUTINE, HOWEVER THAT ALLOWS A LOT OF PRINTABLE CHARACTERS
TO SLIP BY THIS ROUTINE, SO I REWROTE THE CODE TO ONLY ALLOW CONTROL CHARS TO
SLIP BY, FOR MODALDIALOG TO PROCESS
if ((theChar >= kPrintCharStart) && (theChar <= kPrintCharEnd)
&& (theChar != kDeleteKey))
**************************************************************************************/
// New check just for ctrl chars
if (((unsigned short)theChar > kCtrlCharEnd) && (theChar != kDeleteKey))
{
iHit = ((DialogPeek)dialog)->editField + 1;
GetDItem(dialog, iHit, &iType, &iHandle, &iRect);
GetIText(iHandle, iString);
if (iString[0] == kMaxCharLimit){
// Exceeds Max Num Chars. Limit
event->what = nullEvent;
SysBeep(10);
}
if (event->what != nullEvent){
theChar = (event->message & charCodeMask);
/* Accept only lowercase chars or numbers,
convert uppercase to lower */
if (theChar < kSmall_a || theChar > kSmall_z){
if (theChar < kNumeralZero || theChar > kNumeralNine)
{
// Check to convert uppercase to lowercase
if (theChar >= kBig_A && theChar <= kBig_Z)
event->message += kUpperToLowerCaseOffset;
else{
// illegal char entered
event->what = nullEvent;
SysBeep(10);
}
}
}
}
}
}
break;
}
}
return (returnVal);
}
/********************* buttonProc **************************/
pascal void buttonProc( DialogPtr theDialog, short theItem) // Outline Default Button
{
short type;
Rect box;
Handle itemHdl;
GetDItem(theDialog, ok, &type, &itemHdl, &box);
PenSize(3,3);
InsetRect( &box, -4, -4);
FrameRoundRect( &box, 16, 16);
PenSize(1,1);
}